//
//  SDPhotoDetailInfoViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/17.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPhotoDetailInfoViewController.h"
#import "SDPhotoFullScreenViewController.h"
#import "SDPaientInfoViewController.h"
#import "SDPhotoInfoViewController.h"
#import "SDPhotoDetailInfoCell.h"
#import "SDPhotoCommentCell.h"
#import "SDPhotoPreview.h"
#import "SDPaientInfoMgr.h"
#import "SDPaientInfoItem.h"
#import "AAPLCameraViewController.h"
#import "SDTabBarController.h"
#import "SDPhotoDetailView.h"

@interface SDPhotoDetailInfoViewController()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView              *collectionView;
@property (nonatomic, strong) NSArray <SDPhotoInfo *>       *photoInfoArray;

@end

@implementation SDPhotoDetailInfoViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initViews];
}

-(void)viewWillAppear:(BOOL)animated
{
    if (_patientId == 0) {
        _photoInfoArray = [[SDPaientInfoMgr shareAGDatabaseManager] selectALL:[SDPhotoInfo class] tableName:kPhotoTableName];
        
    } else {
        _photoInfoArray = [[SDPaientInfoMgr shareAGDatabaseManager] select:[SDPhotoInfo class] tableName:kPhotoTableName where:[NSString stringWithFormat:@"patientId = %ld",_patientId]];
    }
     _collectionView.contentOffset = CGPointMake(kScreenWidth*[self getPhotoIndex], 0);
    [_collectionView reloadData];
    [super viewWillAppear:animated];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)initViews
{
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && self.navBar != nil)
    {
        [self.navBar setTitle:@"照片详情"];
        UIImage* addImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"actionbar_btn_back.png");
        [self.navBar customNaviLeftButton:self action:@selector(onBack:) image:addImg];
        [self.navBar customNaviRightButton:self action:@selector(onCompare:) title:@"对比"];
    }
    
    self.automaticallyAdjustsScrollViewInsets = false;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

    _collectionView = [[UICollectionView alloc] initWithFrame:autoRectTableViewHaveBar(YES) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    
    [self.view addSubview:_collectionView];
}

- (NSInteger)getPhotoIndex
{
    __block NSInteger index = 0;
    if (_photoInfo) {
        [_photoInfoArray enumerateObjectsUsingBlock:^(SDPhotoInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (_photoInfo.id == obj.id) {
                index = idx;
                *stop = YES;
            }
        }];
    }
    return index;
}

- (BOOL)navigationBarHidden
{
    return NO;
}

- (void)onBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onCompare:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:NO];
    
    SDTabBarController* tabBarController = [SDWindowManager defaultManager].getTabBarController;
    [tabBarController.myTabBar selectTabAtIndex:1];
    tabBarController.selectedIndex = 1;
    
    UIViewController* viewController = tabBarController.selectedViewController;
    if ([viewController isKindOfClass:[AAPLCameraViewController class]]) {
        AAPLCameraViewController* cameraVC = (AAPLCameraViewController*)viewController;
        NSIndexPath *indexPath = [[_collectionView indexPathsForVisibleItems] firstObject];
        cameraVC.compareImgeName = [_photoInfoArray objectAtIndex:indexPath.row].originalFileName;
        cameraVC.comparePhotoInfo = [_photoInfoArray objectAtIndex:indexPath.row];
        [cameraVC setCompareImage];
    }
    
//    AAPLCameraViewController* cameraVC = [[AAPLCameraViewController alloc] init];
//    cameraVC.compareImgeName = _photoInfo.originalFileName;
//    [self.navigationController pushViewController:cameraVC animated:YES];
}


#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(CGRectGetWidth(collectionView.bounds), CGRectGetHeight(collectionView.bounds));
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark - UICollectionViewDataSource
//返回分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//返回每个分区的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _photoInfoArray.count;
}

//返回每个item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
    SDPhotoDetailView *photoView = [[SDPhotoDetailView alloc] initWithFrame:cell.bounds];
    photoView.photoInfo = [_photoInfoArray objectAtIndex:indexPath.row];
    [cell addSubview:photoView];
    return cell;
}

@end
